#pragma once

#include "Transf.h"
#include "Primitive.h"
#include <list>

using namespace std;

class Comp
{
public:
	Comp(){};
	Comp(string _id, texture* _tex, TransformationSet* _transf):
		id(_id),tex(_tex),transf(_transf)
		{child = false; matIndex=0;}
	string id;
	unsigned int matIndex;
	vector<material*> mats;
	texture* tex;
	TransformationSet* transf;
	vector<Comp*> childComp;
	vector<Primitive*> childPrim;
	bool child;

	void addComponentChild(Comp* c)
	{
		childComp.push_back(c);
	}

	void addMaterial(material* m)
	{
		mats.push_back(m);
	}
	/*
	void applyMaterial(material* m)
	{
		glEnable(GL_COLOR_MATERIAL);
		glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION,m->emission);
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, m->ambient);
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, m->diffuse);
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, m->specular);
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, m->shininess * 128.0);

	}
	*/
	void draw(material* fatherMat,texture* fatherText)
	{
		glPushMatrix();
		if(transf!=NULL)
			transf->apply();

		
		material* m;
		if(mats[matIndex]->materialID == "inherit")
			m = fatherMat;
		else
			m = mats[matIndex];
		
		texture* t=tex;
		if(tex->textureID == "inherit")
				t=fatherText;
		
		for(unsigned int i=0; i< childComp.size(); ++i)
		{
			childComp[i]->draw(m,t);
		}
		for(unsigned int i=0; i< childPrim.size(); ++i)
		{
			childPrim[i]->draw(m,t);
		}

		glDisable(GL_TEXTURE_2D);

		glPopMatrix();
	}

	void draw()
	{
		glPushMatrix();
		
		if(transf!=NULL)
			transf->apply();
		
		material* m= mats[matIndex];

		for(unsigned int i=0; i< childComp.size(); ++i)
		{
			childComp[i]->draw(m,tex);
		}
		for(unsigned int i=0; i< childPrim.size(); ++i)
			childPrim[i]->draw(m,tex);	
		glPopMatrix();
	}

	void setMatIndex (int i)
	{
		if((size_t)i<mats.size())
			matIndex=i;
	}

	void applyMaterial(int index)
	{
		childPrim[0]->applyMaterial(mats[index]);
	}
	
	void nextMaterial()
	{
		++matIndex;
		if(matIndex >= mats.size())
			matIndex=0;
	}
};